package com.tsxy.mall.service.impl;

import com.tsxy.mall.entity.Book;
import com.tsxy.mall.entity.Cart;
import com.tsxy.mall.mapper.CartMapper;
import com.tsxy.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public Integer insert(Cart cart) {
        //查询有没有这个购物
        Integer count = cartMapper.queryByBookUser(cart);
        if (count != null){
            cart.setCount(count + 1);
            return cartMapper.update(cart);
        }
        return cartMapper.insert(cart);
    }

    @Override
    public List getAll(Integer id) {
        List<Cart> allBook = cartMapper.getAllBook(id);
        List<Cart> usedBook = cartMapper.getUsedBook(id);
        allBook.addAll(usedBook);
        return allBook;
    }

    @Override
    public Cart update(Integer id, Integer count) {
        cartMapper.updateCount(id,count);

        return cartMapper.getById(id);

    }
}
